package com.itboye.shop.me.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.app.MyApplication;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.MoneyShengyuBean;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.YuEPresenter;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.IsUtilUid;
import com.itboye.shop.util.loadutil.MAlert;
import com.itboye.shop.util.loadutil.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 提现
 */
public class TiXianActivity extends BaseActivity implements Observer{
    TextView tvTitle;
    ImageView back;

    private TextView tixian02_num;//提现账号
    private EditText tixian02_money;//提现余额
    private TextView tixian02_yue;//剩余
    private Button tixian02_ok;//确定
    private String zhanghao, yue, shengyu;
    YuEPresenter presenter;
    MoneyShengyuBean bean;
    String choose, qian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_xian);
        tvTitle.setText("提现");

        qian = (String) SPUtils.get(MyApplication.instatnce, null, Const.QIAN, "");

        presenter = new YuEPresenter(this);
        bean = new MoneyShengyuBean();
        presenter.onMoney(IsUtilUid.isUid());
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tixian02_num:
                Intent intent = new Intent(TiXianActivity.this, MyZhangHuActivity.class);
                intent.putExtra("type","tixian");
                startActivityForResult(intent, 101);
                break;
            case R.id.tixian02_ok:
                try {
                    if (tixian02_money.getText().toString().trim().equals("")) {
                        MAlert.alert("请填写具体金额", Gravity.CENTER);
                    } else if (zhanghao == null || zhanghao.equals("")) {
                        MAlert.alert("请选择账户类型", Gravity.CENTER);
                    } else {
                        if (qian.equals("0")||Double.parseDouble(qian)<Double.parseDouble(tixian02_money.getText().toString())) {
                            MAlert.alert("余额不足", Gravity.CENTER);
                        }else{
                            showProgressDialog("请稍后...",true);
                            presenter.onMoneySys(IsUtilUid.isUid(), zhanghao, Double.parseDouble(tixian02_money.getText().toString())*100+"");
                        }
                    }
                }catch (Exception e){

                }
                break;
            default:
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == 102) {
            String name = data.getStringExtra("choose");
            zhanghao = data.getStringExtra("acctivty");
            tixian02_num.setText("请选择账户:" + name);
        }
    }
    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == presenter.Tixian_success) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
                Log.d("byaltitles", yue);
                finish();
            }
            if (entity.getEventType() == presenter.Tixian_fail) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
            }
            if (entity.getEventType() == presenter.Yue_success) {
                bean = (MoneyShengyuBean) entity.getData();
                Log.d("byaltitledatae", bean.getTotal());
                double in = Double.parseDouble(bean.getTotal());
                tixian02_yue.setText("" + in / 100 + "元");
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
